var vm = new Vue({
    el:"#app",
    data:{
        staff:'',
        dept:'',
        role:'',
        position:'',
        updateFlag:''
    },
    methods:{
        getStaff:function () {
            let str = location.search;
            str = str.split("=")[1];

            axios.post("/staff/queryStaffInfo",{staffId:str}).then(
                function (result) {
                    vm.staff = result.data;
                    console.log(vm.staff);
                }, function (err) {
                    console.log(err);
                }
            )
        },
        getDeptList:function () {
            axios.post("/dept/queryDeptListForUpdate").then(
                function(result){
                    vm.dept=result.data;
                    console.log(vm.dept);
                },function (err) {
                    console.log(err);
                }
            )
        },
        getRoleList:function () {
            axios.post("/role/queryRolesByCondition").then(
                function(result){
                    vm.role=result.data;
                    console.log(vm.role);
                },function (err) {
                    console.log(err);
                }
            )
        },
        getPositionList:function () {
            axios.post("/staff/queryPositionList").then(
                function(result){
                    vm.position=result.data;
                    console.log(vm.position);
                },function (err) {
                    console.log(err);
                }
            )
        },
        updateStaff:function (){
            let flag = true;
            let staffId = $("input[name='staffId']").val();
            let name = $("input[name='name']").val();
            let password = $("input[name='password']").val();
            let tel = $("input[name='tel']").val();
            let email = $("input[name='email']").val();
            let sex = $("input[name='sex']").eq(0).attr("checked") === true?"1":"0";
            let age = $("input[name='age']").val();
            let roleName = $(".roleName").val();
            if (roleName === "请选择"){
                flag = false;
            }
            let deptName = $(".deptName").val();
            if (deptName === "请选择"){
                flag = false;
            }
            let position = $(".position").val();
            if (position === "请选择"){
                flag = false;
            }
            if (flag){
                axios.post("/staff/updateStaff",{staffId:staffId,name:name,password:password,tel:tel,email:email,sex:sex,age:age,roleName:roleName,deptName:deptName,position:position}).then(
                    function(result){
                        vm.updateFlag=result.data;
                        console.log(vm.updateFlag);
                        if (vm.updateFlag){
                            alert("修改成功");
                            location.href = "../../../../templates/sysManage/staffManage/staffList.html";
                        }else {
                            alert("修改失败");
                        }
                    },function (err) {
                        console.log(err);
                    }
                )
            }else {
                alert("您的信息还未填写完全");
            }
        }
    },
    //页面加载完自动执行
    mounted(){
        this.getStaff();
        this.getDeptList();
        this.getRoleList();
        this.getPositionList();
    },
    watch:{
        immediate:true,
        position:function (){
            this.$nextTick(function (){
                $(".position>option").each(function (){
                    if($(this).val() === vm.staff.position){
                        $(this).attr("selected",true);
                    }
                });
            })
        },
        dept:function (){
            this.$nextTick(function (){
                $(".deptName>option").each(function (){
                    if($(this).val() === vm.staff.deptName){
                        $(this).attr("selected",true);
                    }
                });
            })
        },
        role:function (){
            this.$nextTick(function (){
                $(".roleName>option").each(function (){
                    if($(this).val() === vm.staff.roleName){
                        $(this).attr("selected",true);
                    }
                });
            })
        }
    }
})